<?php
namespace app\admin\controller;

use app\common\helper\Uploader;
use app\yxwadmin\controller\Admin;
use think\Exception;
use think\Request;

class Upload extends Admin
{
    /**
     * 新增图片
     * @param Request $request
     * @return string|\think\response\Json
     * @throws Exception
     */
    public function image(Request $request)
    {
        $tag = input('tag', null);
        if ($request->isPost()) {
            $data = $request->param();
            $imgpica1 = input('post.image_cover1','');
            $uploader = new Uploader();
            if(!empty($imgpica1)){
                if (empty($data['url'])) {
                    $rts = $uploader->uploadBase64($imgpica1);
                    if(!$url = $rts) {
                        return json(['code' => 0, '添加失败']);
                    }
                }
            }
            $url = isset($url)? $url:$data['url'];
            return json(['code' => 1, '添加成功', 'data'=>['url' => $url, 'tag' => $tag]]);
        } else {
            $this->assign('tag', $tag);
            return $this->view->fetch('upload/image');
        }

    }

}